﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForm
{
    public partial class TonKhoForm : Form
    {

        private Library.BIZ.SanPhamBIZ s;
        private Library.BIZ.ChiTietKhoBIZ kho;
        private List<Library.BIZ.SanPhamBIZ> listsp;
        public TonKhoForm()
        {
            InitializeComponent();
        }

        private void TonKhoForm_Load(object sender, EventArgs e)
        {
            label4.Text = label5.Text = String.Empty;

            listsp = Library.DAL.SanPhamDAL.FindAll(null, null);

            foreach (Library.BIZ.SanPhamBIZ i in listsp)
            {
                comboBox1.Items.Add(i.ID + " | " + i.MaSP + " | " + i.TenSP);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "" && comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Chưa nhập thông tin");
                return;
            }
            else if (textBox1.Text.Trim() != "")
            {
                s = Library.DAL.SanPhamDAL.GetByMaSP(textBox1.Text.Trim());
                if (s == null)
                {
                    MessageBox.Show("Không tìm thấy sản phẩm");
                    return;
                }
            }
            else if (textBox2.Text.Trim() != "")
            {
                int id = 0;
                if (!Int32.TryParse(textBox2.Text.Trim(), out id))
                {
                    MessageBox.Show("ID không hợp lệ");
                    return;
                }
                s = Library.DAL.SanPhamDAL.GetByID(id);
                if (s == null)
                {
                    MessageBox.Show("Không tìm thấy sản phẩm");
                    return;
                }
            }
            else
            {
                string str = comboBox1.Text;
                str = (str.Split('|')[1]).Trim();
                s = Library.DAL.SanPhamDAL.GetByMaSP(str);
            }

            kho = new Library.BIZ.ChiTietKhoBIZ();

            kho.Get(s, dateTimePicker1.Value);

            label4.Text = "Tên SP: " + s.TenSP;
            label5.Text = "Số lượng: " + kho.SoLuong;

            if (!String.IsNullOrEmpty(s.HinhAnh))
            {
                Image image = Image.FromFile(System.Configuration.ConfigurationManager.AppSettings["dirIMG"].ToString() + "//" + s.HinhAnh);
                pictureBox1.Image = image;
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            }
            else
            {
                pictureBox1.Image = null;
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox2.Clear();
            comboBox1.SelectedIndex = -1;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox1.Clear();
            comboBox1.SelectedIndex = -1;
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox1.Clear();
        }
    }
}
